package cn.iocoder.yudao.module.agent.controller.app.brokeragewithdrawrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.agent.controller.admin.brokeragewithdrawrecord.vo.AgentBrokerageWithdrawRecordPageReqVO;
import cn.iocoder.yudao.module.agent.controller.admin.brokeragewithdrawrecord.vo.AgentBrokerageWithdrawRecordRespVO;
import cn.iocoder.yudao.module.agent.controller.app.brokeragewithdrawrecord.vo.AppAgentBrokerageWithdrawRecordCreateReqVO;
import cn.iocoder.yudao.module.agent.convert.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordConvert;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordDO;
import cn.iocoder.yudao.module.agent.dal.dataobject.withdrawaccount.AgentWithdrawAccountDO;
import cn.iocoder.yudao.module.agent.service.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordService;
import cn.iocoder.yudao.module.agent.service.withdrawaccount.AgentWithdrawAccountService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "APP - 代理人佣金提现记录")
@RestController
@RequestMapping("/agent/brokerage-withdraw-record")
@Validated
public class AppAgentBrokerageWithdrawRecordController {

    @Resource
    private AgentBrokerageWithdrawRecordService agentBrokerageWithdrawRecordService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private AgentWithdrawAccountService withdrawAccountService;

    @GetMapping("/page")
    @Operation(summary = "获得分销提现分页")
    @PreAuthenticated
    public CommonResult<PageResult<AgentBrokerageWithdrawRecordRespVO>> getBrokerageWithdrawPage(AgentBrokerageWithdrawRecordPageReqVO pageReqVO) {
        pageReqVO.setUserId(getLoginUserId());

        PageResult<AgentBrokerageWithdrawRecordDO> pageResult = agentBrokerageWithdrawRecordService.getBrokerageWithdrawRecordPage(pageReqVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), AgentBrokerageWithdrawRecordDO::getUserId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        List<AgentWithdrawAccountDO> accountList = withdrawAccountService.getWithdrawAccountListByUserIds(memberIds);
        Map<Long, AgentWithdrawAccountDO> accountMap = convertMap(accountList, AgentWithdrawAccountDO::getUserId);

        return success(AgentBrokerageWithdrawRecordConvert.INSTANCE.convertPage02(pageResult, memberMap, accountMap));
    }

    @PostMapping("/create")
    @Operation(summary = "代理创建佣金提现请求")
    @PreAuthenticated
    public CommonResult<Long> createBrokerageWithdraw(@RequestBody @Valid AppAgentBrokerageWithdrawRecordCreateReqVO createReqVO) {
        return success(agentBrokerageWithdrawRecordService.appCreateBrokerageWithdrawRecord(createReqVO));
    }
}
